/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function buscarTipo(){
    if($("#buscar").val().length==0){
        return;
    }
    $.ajax(
    {
        type: "GET",
        url: "buscarTipo",   
        async: false,
        dataType: "text",
        data: "buscar="+$("#buscar").val(),
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            $("#body-content").html(transport);
            $('input[id="btnVer"]').click(verTipo);
            $('input[id="btnEliminar"]').click(eliminarTipo);
            $('input[id="btnModificar"]').click(modificarTipo);
            $('#btnAgregar').click(agregarTipo);
            $('#btnBuscar').click(buscarTipo);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
}

function modificarTipoA(){
    if(!validaAltaTipo()){
        alert("Campos invalidos");
        return false;
    }
        
    $.ajax(
    {
        type: "POST",
        url: "modificarTipo",
        async: false,
        dataType: "text",
        data: $('#modficarTipoF').serialize(),
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            alert(transport);
            $("#body-content").html("");
            $('#menuTipo').trigger('click');
                               
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
    $('#menuTipo').trigger('click');
    $('#menuTipo').trigger('click');
    return false;                        
}

function modificarTipo(){    
    $("#tablaTipo").hide();
    
    $.ajax(
    {
        type: "GET",
        url: "gestionModificarTipoPre",
        async: false,
        dataType: "text",
        data: "idTipo="+$(this).closest('tr').attr("id"),
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            $("#verTipo").html(transport);
            $(".error-inner").hide();
            $("#verTipo").show();
            $("#tablaTipo").hide();
            $('#btnModificarTipo').click(modificarTipoA);
            $("#btnRegresar").click(mostrarTipo);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
}

function eliminarTipo(){ 
     id = $(this).closest('tr').attr("id");
    if(!confirm("Seguro que desea eliminar el empleado "+ id))
        return;   
    $.ajax(
    {
        type: "GET",
        url: "eliminarTipo",
        async: false,
        dataType: "text",
        data: "idTipo="+ id,
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            alert(transport); 
            $("#"+id).remove()
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
}

function mostrarTipo(){
    $("#verTipo").hide();   
    $("#tablaTipo").show();
}

function agregarTipo(){  
    $("#tablaTipo").hide();
    
    $.ajax(
    {
        type: "GET",
        url: "GestionTipos/gestionAgregarTipo.jsp",
        async: false,
        dataType: "text",
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            $("#verTipo").html(transport);
            $(".error-inner").hide();
            $("#verTipo").show();
            $("#tablaTipo").hide();
            $('#btnAltaTipo').click(altaTipo);
            $("#btnRegresar").click(mostrarTipo);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
}

function altaTipo(){
    if(!validaAltaTipo())
        return false;
       
    $.ajax(
    {
        type: "POST",
        url: "altaTipo",
        async: false,
        dataType: "text",
        data: $('#altaTipo').serialize(),
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            alert(transport);
            $("#body-content").html("");
            $('#menuTipo').trigger('click');
                               
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
    $('#menuTipo').trigger('click');
    $('#menuTipo').trigger('click');
    return false;
}

function validaAltaTipo(){
    $('.error-inner').hide();
    esValido = true;
    if($('#Ntipo').val().length<3){
        $("#Ntipoe").show();
        esValido = false;
    }    
    return esValido;
}
